#!/bin/sh

ifconfig eth1 mtu 1360
###########################################################
#             DEFINE FILES AND VARIABLES
###########################################################
chmod +x /usr/bin/logather.sh
chmod +x /usr/bin/scpbbu
BIN_DIR="/usr/bin"
LOG_DIR="/mnt/usr/log"
FILE_DIR="/mnt/usr/"
LAST_LOG="/mnt/usr/log.tar.gz"
PRE_LOG="/mnt/usr/prelog.tar.gz"
FWM_UPGRADE_DIR="/mnt/usr/.upgrade"
FWM_INSTALLING="${FWM_UPGRADE_DIR}/.installing"
FWM_X86APP_IMG_NAME="x86proj_1.0-1.ipk"
export x86Ipaddr=192.168.178.111
export x86port=32790
###########################################################
#                     FUNCTIONS
###########################################################

###########################################################
#  Logging
###########################################################
LOG_INFO () 
{
  logger -p local0.notice $LOGGER_V -t lanuchapp **INFO** "$*"
}
LOG_ERR () 
{
  logger -p local0.err $LOGGER_V -t lanuchapp **ERROR** "$*"
}
LOG_CRIT () 
{
  logger -p local0.crit $LOGGER_V -t lanuchapp **CRITICAL** "$*"
}

###########################################################
#  Shell command wrappers - mainly used to log errors
###########################################################
SHELL_ERR_LOG()
{
  output=`$1 2>&1`
  local rc=$?
  if [ $rc -ne 0 ]; then
    LOG_ERR $1 "command output: ($output), command rc ($rc)"
  fi
  return $rc
}
SHELL_CRIT_LOG()
{
  output=`$1 2>&1`
  local rc=$?
  if [ $rc -ne 0 ]; then
    LOG_CRIT $1 "command output: ($output), command rc ($rc)"
  fi
  return $rc
}

###########################################################
#                      MAIN
###########################################################

ulimit -s unlimited
ulimit -c unlimited

#Change net parameter of kernel
sysctl -w net.core.rmem_max=16777216
sysctl -w net.core.rmem_default=16777216
sysctl -w net.core.wmem_max=16777216
sysctl -w net.core.wmem_default=16777216
sysctl -w net.unix.max_dgram_qlen=256

if [ -f $FWM_INSTALLING ] ; then
    opkg --force-overwrite install $FWM_UPGRADE_DIR/$FWM_X86APP_IMG_NAME > /dev/null 
    if [ $? -ne 0 ]; then
      CRIT_LOG "unable install x86app"    
      exit 1
    fi
    SHELL_CRIT_LOG "rm $FWM_INSTALLING" 
    SHELL_CRIT_LOG "sync"
    SHELL_CRIT_LOG "reboot -f"   
fi

if [ -f /mnt/usr/network ] ; then
	cp /mnt/usr/network /etc/config/
fi

if [ ! -e $LOG_DIR ] ; then
    SHELL_CRIT_LOG "mkdir -p $LOG_DIR"
fi
cd $FILE_DIR
if [ -f $PRE_LOG ] ;then
     rm $PRE_LOG
fi

if [ -f $LAST_LOG ] ;then
   mv $LAST_LOG $PRE_LOG
fi
cd $LOG_DIR
tar -zcvf $LAST_LOG  *
rm  bes.log.last fep.log.old  3gdongle.log.old  
sleep 5    
cd $BIN_DIR
$BIN_DIR/bes &

sleep 2

$BIN_DIR/3gdongle -l 5 &
$BIN_DIR/fwmgr >& $LOG_DIR/fwmgr_scr.log &

sleep 7

$BIN_DIR/fep -l 5 &
ifup wan1
LOG_INFO "Launch application done!"
exit 0
